using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// author:Zeng shunbo
    /// </summary>
    public partial class AssetTypeList : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140801";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(AssetTypeList_ChangeLanguage);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdAssetTypeList.PageSize = this.GridViewPageDefaultSize();
                SetLanguage();
                Display();
            }
        }

        #endregion

        #region SetLanguage

        void AssetTypeList_ChangeLanguage(object sender, EventArgs e)
        {
            SetLanguage();
            Display();
        }

        private void SetLanguage()
        {
            ChangeGridHead();
            ChangeLabelOrButton();
        }

        private void ChangeLabelOrButton()
        {
            this.lblStation.Text = GetLabel("SOM_Station", true);
            this.lblAssentType.Text = GetLabel("SOM_AssetType", true);
            this.lblObsolete.Text = GetLabel("COM_Show", true);
            this.btnSearch.Text = GetLabel("COM_Search");
            this.btnAddNew.Text = GetLabel("SOM_AddNewAssetLoanItem");
            this.chkObsolete.Text = GetLabel("SOM_Voided");
        }

        private void ChangeGridHead()
        {
            this.grdAssetTypeList.Columns[1].HeaderText = GetLabel("SOM_Station");
            this.grdAssetTypeList.Columns[2].HeaderText = GetLabel("SOM_AssetType");
            this.grdAssetTypeList.Columns[3].HeaderText = GetLabel("COM_Description");
            this.grdAssetTypeList.Columns[4].HeaderText = GetLabel("SOM_Void");
            this.grdAssetTypeList.Columns[5].HeaderText = GetLabel("COM_Edit");
        }

        #endregion

        #region Some private function

        private void Display()
        {
            FunctionItemSetting();
            BindAssetTypeList();
        }

        /// <summary>
        /// setting user access by function item.
        /// </summary>
        private void FunctionItemSetting()
        {
            if (DAL.Security.User.CanFunctionItemAccess("M140801_F1", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140801_F4", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
            }
            else
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
                this.ddlStation.Enabled = false;
            }

            if (DAL.Security.User.CanFunctionItemAccess("M140801_F2", this.ODMUser.UserProfileID, this.MenuID)) //readonly
            {
                this.btnAddNew.Enabled = false;
            }

            //update by gordon 20151020 U247
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)
            //{
            //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString(); 
            //}
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)
            {
                this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
            }
        }

        private void BindAssetTypeList()
        {
            //BindGrid
            this.grdAssetTypeList.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            string stationIDs = "";
            SetStationSelectedItem(ref stationIDs, this.ddlStation);

            DataTable assetTypeTable = MTRC.ODMS.DAL.SOM.CodeSetup.CDAssetType.GetAssetTypeList(stationIDs, this.txtAssetType.Text, this.chkObsolete.Checked, this.LangCode);
            //if (assetTypeTable.Rows.Count < 1)
            //{
            //    assetTypeTable.Rows.Add(assetTypeTable.NewRow());
            //}
            DataView dvAssetType = assetTypeTable.DefaultView;
            dvAssetType.RowFilter = "FlagObsolete is not null";
            dvAssetType.Sort = this.grdAssetTypeList.DataViewSortExpression;
            this.grdAssetTypeList.DataSource = dvAssetType;
            this.grdAssetTypeList.DataBind();

            assetTypeTable.Dispose();
            assetTypeTable = null;
        }

        #endregion

        #region Some Event

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AssetTypeEntry.aspx?action=new");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindAssetTypeList();
        }

        protected void grdAssetTypeList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == ("edit").ToUpper())
            {
                this.Response.Redirect("AssetTypeEntry.aspx?action=edit&AssetTypeID=" + e.CommandArgument.ToString());
            }
        }

        protected void grdAssetTypeList_NeedDataBind()
        {
            BindAssetTypeList();
        }

        #endregion
    }
}
